package org.shop.controller.app.live;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.ClassInfo;
import org.shop.entity.PostInfo;
import org.shop.entity.Question;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.AnswerInfoService;
import org.shop.service.ClassInfoService;
import org.shop.service.ForumService;
import org.shop.service.PraiseService;
import org.shop.service.QuestionAnswerService;
import org.shop.service.QuestionService;
import org.shop.util.CommonUtil;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class QuestionContrller {
	private static Logger logger = LogManager.getLogger(QuestionContrller.class);
	
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	
    /**
     * app问答发帖
     * @param req
     * @return
     */
    @RequestMapping("app/addQuestion")
    @ResponseBody
    public Map<String, Object> addQuestion(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
            String content = req.getParameter("content");
            String contentImg = req.getParameter("contentImg");
            String textBlackList=CommonUtil.getPropertiesParam("textBlackList");
            for(String textBlack:textBlackList.split(",")){
                if(content.contains(textBlack)){
                    map.put("state", 2);
                    map.put("msg", "发布内容不合法");
                    return map;
                }
            }
            MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Question question = new Question();
            question.setContent(content);
            question.setContentImg(contentImg);
            question.setSender(ud.getUserId());
            questionService.insertSelective(question);
            map.put("state", 1);
            map.put("msg", "发布成功");
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "发布失败");
        }
        return map;
    }
    
    /**
     * app获取问答所有贴子列表
     * @param req
     * @return
     */
    @RequestMapping("app/getQuestionList")
    @ResponseBody
    public List<Map<String, Object>> getQuestionList(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
    	String content = req.getParameter("content");
//    	String classId = req.getParameter("classId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
    	Map<String, Object> params = new HashMap<String, Object>();
    	List<Map<String, Object>> getQuestionList2 = null;
    	try {
    		params.put("content", content);
        	if(ObjUtil.isNotNullAndEmpty(ud)) {
        		params.put("userId", ud.getUserId());
        	}
    		params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
    		params.put("pageSize", pageSize);
        	List<Map<String, Object>> getQuestionList = questionService.getQuestionList(params);
        	getQuestionList2 = new ArrayList<Map<String,Object>>();
        	for(int i=0;i<getQuestionList.size();i++) {
        		Map<String, Object> map = getQuestionList.get(i);
        		Map<String, Object> m = new HashMap<String, Object>();
        		m.put("questionId", map.get("questionId"));
        		map.put("questionAnswerCount", questionAnswerService.getQuestionAnswerCount(m));
        		getQuestionList2.add(map);
        	}
    	}catch(Exception e) {
    		e.printStackTrace();
    		logger.error(e.getMessage());
    	}
    	
        return getQuestionList2;
    }
    
    /**
     * app查看问答详情
     * @param req
     * @return
     */
    @RequestMapping("app/getQuestionDetails")
    public String getQuestionDetails(HttpServletRequest req){
    	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
		String questionId = req.getParameter("questionId");
		
		//根据贴子id获取问答详情
    	Map<String, Object> params = new HashMap<String, Object>();
    	try {
    		params.put("questionId", questionId);
        	if(ObjUtil.isNotNullAndEmpty(ud)) {
        		params.put("userId", ud.getUserId());
        		req.setAttribute("userInfo", ud);
        	}
        	Map<String, Object> questionDetails = questionService.getQuestionDetails(params);
        	req.setAttribute("questionDetails", questionDetails);
    	}catch(Exception e) {
    		e.printStackTrace();
    		logger.error(e.getMessage());
    	}
    	return "app/find/questionsAnswersDetails";
    }
    
}
